package com.woniuxy.kaer.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.system.dto.RoleResourceDTO;
import com.woniuxy.kaer.system.mapper.ResourceMapper;
import com.woniuxy.kaer.system.mapper.RoleMapper;

import com.woniuxy.kaer.system.model.Resources;
import com.woniuxy.kaer.system.model.Role;
import com.woniuxy.kaer.system.model.RoleResource;

import com.woniuxy.kaer.system.mapper.RoleResourceMapper;
import com.woniuxy.kaer.system.service.ResourceService;
import com.woniuxy.kaer.system.service.RoleResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.kaer.system.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class RoleResourceServiceImpl extends ServiceImpl<RoleResourceMapper, RoleResource> implements RoleResourceService {

    @Resource
    private RoleResourceMapper roleResourceMapper;
    @Resource
    private RoleService roleService;
    @Resource
    private ResourceService resourceService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private ResourceMapper resourceMapper;




    @Override
    public void addRoleResource(Integer roleId, Integer resourceId) throws Exception {
        final QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Role_id", roleId);
        Role role = roleMapper.selectOne(queryWrapper);
        if (role == null) {
            throw new Exception("角色不存在");
        }
        final QueryWrapper<Resources> ResourceQueryWrapper = new QueryWrapper<>();
        ResourceQueryWrapper.eq("Resource_id", resourceId);
        Resources resource =  resourceMapper.selectOne(ResourceQueryWrapper);
        if (resource == null) {
            throw new Exception("资源不存在");
        }
        final QueryWrapper<RoleResource> roleResourceQueryWrapper = new QueryWrapper<>();
        roleResourceQueryWrapper.eq("role_id", roleId).eq("resource_id", resourceId);
        if (roleResourceMapper.selectOne(roleResourceQueryWrapper)!=null) {
            throw new Exception("角色资源关联已存在");
        }
        RoleResource roleResource = new RoleResource();
        roleResource.setRoleId(roleId);
        roleResource.setResourceId(resourceId);
        roleResourceMapper.insert(roleResource);
    }

    @Override
    public void removeRoleResource(Integer roleId, Integer resourceId) throws Exception {
        final QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Role_id", roleId);
        Role role = roleMapper.selectOne(queryWrapper);
        if (role == null) {
            throw new Exception("角色不存在");
        }
        final QueryWrapper<Resources> ResourceQueryWrapper = new QueryWrapper<>();
        ResourceQueryWrapper.eq("Resource_id", resourceId);
        Resources resource=resourceMapper.selectOne(ResourceQueryWrapper);
        if (resource == null) {
            throw new Exception("资源不存在");
        }

        final QueryWrapper<RoleResource> roleResourceQueryWrapper = new QueryWrapper<>();
        roleResourceQueryWrapper.eq("role_id", roleId).eq("resource_id", resourceId);
        RoleResource roleResource = roleResourceMapper.selectOne(roleResourceQueryWrapper);
        if (roleResource == null) {
            throw new Exception("角色资源关联不存在");
        }
        roleResourceMapper.deleteById(roleResource.getResourceAccount());
    }

    @Override
    public void updateRoleResource(RoleResourceDTO roleResourceDTO) throws Exception {
        Integer roleId = roleResourceDTO.getRoleId();
        //删除该角色的所有权限
        QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        roleResourceMapper.delete(queryWrapper);
        //添加新的权限
        List<Integer> resourceIds = roleResourceDTO.getResourceIds();
        for (Integer resourceId : resourceIds) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            roleResourceMapper.insert(roleResource);
        }
        //添加新的权限
//        //获取角色ID
//        QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_id", roleId);
//        //获取角色资源关联
//        List<RoleResource> roleResources = roleResourceMapper.selectList(queryWrapper);
//        if (roleResources == null) {
//            throw new Exception("该角色没有资源关联");
//        }
//        //判断操作类型(增加或减少)
//        if (roleResources.size() < roleResourceDTO.getResourceIds().size()) {
//            //增加
//            for (Integer resourceId : roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList())) {
//                //判断是否已有关联
//                if (!roleResources.contains(resourceId)) {
//                    //增加关联
//                    addRoleResource(roleId, resourceId);
//                }
//            }
//
//        } else {
//            //减少
//            for (Integer resourceId : roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList())) {
//                //判断是否已有关联
//                if (!roleResourceDTO.getResourceIds().contains(resourceId)) {
//                    //删除关联
//                    removeRoleResource(roleId, resourceId);
//
//                }
//            }
//        }
    }

    @Override
    public List<Resources> getRoleResource(Integer roleId) throws Exception {
        //判断该角色是否存在
        final QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Role_id", roleId);
        Role role = roleMapper.selectOne(queryWrapper);
        if (role == null) {
            throw new Exception("角色不存在");
        }
        //获取角色资源关联
        final QueryWrapper<RoleResource> roleResourceQueryWrapper = new QueryWrapper<>();
        final ArrayList<Resources> list = new ArrayList<>();
        roleResourceQueryWrapper.eq("role_id", roleId);
        List<RoleResource> roleResources = roleResourceMapper.selectList(roleResourceQueryWrapper);
        if(roleResources.size()==0){
            return list;
        }
        //获取资源
        List<Integer> resourceIds=roleResources.stream().map(RoleResource::getResourceId).collect(Collectors.toList());
        final QueryWrapper<Resources> resourceQueryWrapper = new QueryWrapper<>();
        resourceQueryWrapper.in("Resource_id", resourceIds);
        List<Resources> resources = resourceMapper.selectList(resourceQueryWrapper);
        list.addAll(resources);
        return list;

    }



}
